package com.test.controller;

import com.test.Aspect.ServerConfig;
import com.test.enums.Replay;
import com.test.utils.ReplayUtil;
import com.test.utils.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * PhotoController
 *
 * @author HY
 * 2021/05/14 11:36
 **/

@RestController
@RequestMapping("/upload")
@Api("上传文件接口")
public class UploadController {

    @Autowired
    private ServerConfig serverConfig;

    @ApiOperation("上传图片并返回图片地址")
    @PostMapping(value = "/photoOfURL")
    public Replay UploadPhoto(@RequestParam("file") MultipartFile file, HttpServletRequest request) {

        UploadUtil uploadUtil = new UploadUtil();
        //保存上传图片至指定路径
        uploadUtil.doUpload(file, request, "images/stores");

        //拼接图片地址
        String url = serverConfig.getUrl() + uploadUtil.getUploadFile();

        return ReplayUtil.success(url);
    }

    @ApiOperation("上传图片并提示(具体业务，待定)")
    @PostMapping(value = "/photo")
    public Replay testUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {

        UploadUtil uploadUtil = new UploadUtil();
        //
        uploadUtil.doUpload(file, request, "images/stores");

        return ReplayUtil.success();
    }

}
